<?php
header('Content-type:text/html;charset=utf8');
/**
 * xysc users Version1.0
 * ==================================
 * Copy 2016-2017
 * Web:www.xjh7.com
 * ==================================
 * Author:xjh
 * Date:2016-3-25
*/
/**
 * 
 * 店铺控制器
 * @author xjh
 *
 */
class StoreController extends Controller{
	/**
	 * 访问控制过滤
	 * @author xjh
	 */
	function filters(){
		return array(
			'accessControl'
		);
	}
	/**
	 * 具体方法访问具体条件
	 */
	
	function accessRules(){
		return array(
			array(
				'allow',
				'actions'=>array(''),
				'users'=>array('@'),//登陆系统的用户
			),
//			array(
//				'deny',
//				'users'=>array('*'),//所有用户
//			),
		);
	}
	/**
	 * 顶部信息
	 * 
	 */
	function actionStoreTop(){
		$this->renderPartial('storeTop');
	}
	/**
	 * 店铺删除
	 */
	function actionStoreDel($store_id){
		$store_model = Store::model();
		$row = $store_model->storeDel($store_id);
		if($row){
			echo "<script>alert('删除成功');</script>";
			$this->renderPartial('storetop');
		}else{
			echo "<script>alert('删除失败');</script>";
			$this->renderPartial('storetop');
		}
	}
	/**
	 * 店铺修改
	 */
	function actionStoreUpd($store_id){
		$recPost = $_POST;
		$store_model = new Store();
		$user_model = User::model();
		if(Yii::app()->user->name){
			$lg_model = new LoginForm();
			$role_name = $lg_model->getRoleName();
			$lg_id = $lg_model->getUserId(Yii::app()->user->name);
		}
	
		
		$user_info = $user_model->getAllSal();
		$info = $store_model->getOneStore($store_id);
		$info = $info[0];
		$u = array();
		foreach($user_info as $_k => $_v){
			$u[$_v['user_id']] = $_v['user_name'];
		}
		if(isset($recPost) && @$recPost['send'] == "修改"){
			$row = $store_model->StoreUpdate($recPost,$store_id);
			if($row){
				echo "<script>alert('修改成功');</script>";
				$this->renderPartial('storetop');
			}else{
				echo "<script>alert('修改失败');</script>";
			}
		}
		$this->renderPartial('updatestore',array('info'=>$info,'u_name'=>$u,'user_info'=>$user_info,'user_type'=>$role_name[0]['role_name'],'login_name'=>Yii::app()->user->name,'login_id'=>$lg_id[0]['user_id']));
	}

	
	/**
	 * 是否有效的判断
	 */
	function actionActive($store_id,$active_id){
		$active = new active();
		$active->setActive($store_id,$active_id,"store","store_id");
		$this->redirect("./index.php?r=backweb/store/store");
	}
	
	/**
	 * 店铺控制器
	 * @author xjh
	 */
	function actionStore($store_id = 0){
		$store_model = Store::model();
		if(Yii::app()->user->name != ''){
			$lg_model = new LoginForm();
			$role_name = $lg_model->getRoleName();
		}
		$user_name = Yii::app()->user->name;
		$cmodel = new CCCModel();
		$rst = $store_model->pageSearch($store_id,$role_name[0]['role_name'],Yii::app()->user->name);
		$page_list = $store_model->page_list;
		$infos_count = $store_model->infos_count;
		$user_info = $store_model->getOwnName();
		
		$u = array();
		foreach($user_info as $_k => $_v){
			$u[$_v['user_id']] = $_v['user_name'];
		}
		
		$obj = new CDbConnection();
		$topInfo = $obj->getStats();
		$number = $topInfo[0];
		$times = $topInfo[1];
		$active = array(
			'1' => '是',
			'0' => '否',
		);
		$this->renderPartial('store',array('all_info'=>$rst,'u_name'=>$u,'info_count'=>$infos_count[0]['count(*)'],'number'=>$number,'times'=>$times,'active'=>$active,'page_list'=>$page_list,'user_type'=>$role_name[0]['role_name']));
	}
	/**
	 * 推荐信息
	 */
	
	function actionstorerec($goods_id = 0,$rec = 0){
		$goods_model = Goods::model(); 
		if(Yii::app()->user->name){
			$lg_model = new LoginForm();
			$role_name = $lg_model->getRoleName();
		}
		
		$goods_model->rec($goods_id,$rec);
		
		
		$goods_info= $goods_model -> getGoodsByTypeName($role_name[0]['role_name']);
		$goods_name = $goods_model -> getGoodsNameByTypeName($role_name[0]['role_name']);
		$goodsname = array();
		//重新拼装商品名
		foreach ($goods_name as $_v){
			$goodsname[$_v['goods_id']] = $_v['goods_name'];
		} 
		$this->renderPartial('storerec',array('goods_info'=>$goods_info,'goodsname'=>$goodsname));
	}
	
	/**
	 * 店铺排序设定
	 */
	function actionSort(){
		if(isset($_POST) && $_POST['send'] == "排序"){
			$store_model = new Store();
			$dataAll = array_combine($_POST['store_id'],$_POST['sort']);
			$store_model->sort($dataAll);
			$this->redirect("./index.php?r=backweb/store/store");
		}
	}

	
	function actiontoAddStore(){
		$user_model = User::model();
		$user_info = $user_model -> getAllSal();
		if(Yii::app()->user->name){
			$lg_model = new LoginForm();
			$role_name = $lg_model->getRoleName();
			$lg_id = $lg_model->getUserId(Yii::app()->user->name);
		}
		$this->renderPartial('addstore',array('user_info'=>$user_info,'user_type'=>$role_name[0]['role_name'],'login_name'=>Yii::app()->user->name,'login_id'=>$lg_id[0]['user_id']));
	}
	/*
	 * 店铺添加控制器
	 * @author xjh
	 */
	function actionAddStore(){
		$addInfo  = $_POST;
		$store_model = Store::model();
		$user_model = User::model();
		$infos = $store_model->checkExist($addInfo);
		if(isset($addInfo)){
			if($_POST['send'] == '添加'){
				if(!empty($infos)){
					echo "<script>alert('该店铺已存在');</script>";
					echo "操作错误，请返回";
					exit;
				}
				if($store_model->addStore($addInfo)){
					echo "<script>alert('店铺添加成功');</script>";
					 $this->renderPartial('storeTop');
				}else{
					echo "<script>alert('添加失败');</script>";
				}
			}
		}
		
	}
}
?>